package com.example.qxapp.Fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.qxapp.Bean.User;
import com.example.qxapp.R;
import com.example.qxapp.activity.MycollectActivity;
import com.example.qxapp.activity.MycomunityActivity;
import com.example.qxapp.activity.MyinfoActivity;
import com.example.qxapp.activity.MypushActivity;
import com.example.qxapp.activity.SettingActivity;
import com.example.qxapp.activity.dlActivity;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

public class Fragment3 extends androidx.fragment.app.Fragment {
    private TextView username,fansnum,myfocusnum;
    private ImageView mine_gender;
    private LinearLayout followactivity,focusactivity,mypush,mycomunity,mycollect,setting,myinfo;
    private FloatingActionButton floatingActionButton;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.tab_my,container,false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        init();
       getmyinfo();
        floatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BmobUser.logOut();
                startActivity(new Intent(getActivity(), dlActivity.class));
                getActivity().finish();
            }
        });


    }

    private void getmyinfo() {
        //加载个人信息
        BmobUser bu=BmobUser.getCurrentUser(BmobUser.class);
        String id = bu.getObjectId();
        BmobQuery<User> bmobQuery=new BmobQuery<>();
        bmobQuery.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e==null){
                    username.setText(user.getUsername());
                }else {
                    Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
                }
            }
        });

        myinfo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MyinfoActivity.class));
            }
        });

        mypush.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MypushActivity.class));
            }
        });

        mycomunity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MycomunityActivity.class));
            }
        });

        mycollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), MycollectActivity.class));
            }
        });
        setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), SettingActivity.class));
            }
        });
    }

    private void init() {
        username = getActivity().findViewById(R.id.username);
        mypush = getActivity().findViewById(R.id.mypush);
        mycomunity = getActivity().findViewById(R.id.mycomunity);
        mycollect = getActivity().findViewById(R.id.mycollect);
        mine_gender = getActivity().findViewById(R.id.mine_gender);
        fansnum = getActivity().findViewById(R.id.fansnum);
        setting = getActivity().findViewById(R.id.setting);
        myfocusnum = getActivity().findViewById(R.id.myfocusnum);
        followactivity = getActivity().findViewById(R.id.followactivity);
        focusactivity = getActivity().findViewById(R.id.focusactivity);
        floatingActionButton=getActivity().findViewById(R.id.floatingActionButton);
        myinfo=getActivity().findViewById(R.id.myinfo);
    }
}
